package com.rong.action;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionSupport;
import com.rong.DAO.ZynParaDAO;
import com.rong.ORM.ZynPara;
import com.rong.hibernate.BaseHibernateDAO;

public class ParaAction extends ActionSupport {
	ZynPara para=new ZynPara();
	ZynParaDAO paraDao=new ZynParaDAO();
	private List<ZynPara> list;
	private String saveFlag="";
	private int id=-1; // the Para id indatabase.
	 BaseHibernateDAO b=new BaseHibernateDAO();
	/**
	 * @return the para
	 */
	public ZynPara getPara() {
		return para;
	}
	/**
	 * @param para the para to set
	 */
	public void setPara(ZynPara para) {
		this.para = para;
	}
	/**
	 * @return the list
	 */
	public List<ZynPara> getList() {
		return list;
	}
	/**
	 * @param list the list to set
	 */
	public void setList(List<ZynPara> list) {
		this.list = list;
	}
	/**
	 * @return the saveFlag
	 */
	public String getSaveFlag() {
		return saveFlag;
	}
	/**
	 * @param saveFlag the saveFlag to set
	 */
	public void setSaveFlag(String saveFlag) {
		this.saveFlag = saveFlag;
	}
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	@Override
    public String execute() throws Exception {
        this.setSaveFlag("");
		paraDao.save(this.para);        //保存注册的para对象
		 this.setSaveFlag("保存成功..");
        return SUCCESS;
    }
	 public String findall() throws Exception {
	        
		 this.setList(paraDao.findAll());        //保存注册的user对象
	        return "success_list";
	    }
	 public String findbyid() throws Exception {
	        
		this.setPara(paraDao.findById(this.getId()));// find the para by id			
	        return "s_find_one";
	       
	    }
	 public int findbydate(Date billDate) throws Exception {  // 根据日期选择当时有效的系统参数
			int paraId=-1;
		 	Session s=null;
	         s=b.getSession();
			Criteria c_oil=s.createCriteria(ZynPara.class);
        	c_oil.add(Restrictions.and(Restrictions.isNotNull("oilprice"),Restrictions.gt("invalidationDate", billDate) ) ); //日期大于当下日期			        
	         List<ZynPara> listoil=c_oil.list();	
	         if(listoil.size()==0){//取当前日期的参数
	        	 Criteria prac=s.createCriteria(ZynPara.class);
	        	 prac.add(Restrictions.and(Restrictions.isNotNull("oilprice"),Restrictions.isNull("invalidationDate") ) ); //日期为空    
	        	 List<ZynPara> l_para=prac.list();	
	        	 if(l_para.size()==0){
	        		 paraId=-1;             //没有对应参数取-2，在报表中进行右连接，price为空再提示
	        	 }else{
	        		 paraId=l_para.get(0).getId(); 
	        	 }
	         }else{
	        	 paraId=listoil.get(0).getId();
	         }

		        return paraId;
		       
		    }
	 
	 public String edit() throws Exception {
	      			
		   paraDao.merge(this.getPara());// edit the Para by id			
		    return "s_edit_one";
		    }
	 public String delete() throws Exception {
	        
			paraDao.delete(paraDao.findById(this.getId()));// delete the para by id			
		    return "s_delete_one";
		    }
}
